package cc.rengu.igas.route.common.dao.impl;

import cc.rengu.igas.route.common.dao.NonFinanceTxnRouteCustomMapper;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.NonFinanceTxn;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 非金融交易流水表操作接口实现类
 * Created by lizhangtao on 2020/08/05.
 *
 * @author lizhangtao
 * @version 1.0
 */
public class NonFinanceTxnRouteCustomMapperImpl implements NonFinanceTxnRouteCustomMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public NonFinanceTxnRouteCustomMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public List<NonFinanceTxn> fetchTxnByFrontSeqNumWithNoDateTime(String instId, String txnDate, String frontSeqNum, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "fetchNonTxnByFrontSeqNumWithNoDateTime");
        NonFinanceTxn nonFinanceTxn = new NonFinanceTxn();
        nonFinanceTxn.setInstId(instId);
        nonFinanceTxn.setTxnDate(txnDate);
        nonFinanceTxn.setFrontSeqNum(frontSeqNum);
        nonFinanceTxn.setMsgSrcId(msgSrcId);
        nonFinanceTxn.setTransChannelId(transChannelId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, nonFinanceTxn, NonFinanceTxn.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据前端流水号不带日期查询非金融交易流水表失败,instId:<{}>,frontSeqNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,txnDate:<{}>",
                    instId, frontSeqNum, msgSrcId, transChannelId, txnDate);
            return null;
        }
        return objectList.stream().map(item -> (NonFinanceTxn) item).collect(Collectors.toList());
    }

    @Override
    public void updDevBrandByPriKey(String instId, String sysSeqNum, String dstChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updNonTxnDevBrandByPriKey");
        NonFinanceTxn nonFinanceTxn = new NonFinanceTxn();
        nonFinanceTxn.setInstId(instId);
        nonFinanceTxn.setSysSeqNum(sysSeqNum);
        nonFinanceTxn.setDevBrand(dstChannelId);
        int retCode = dbsUtil.dbsExecuteSql(sqlStr, nonFinanceTxn, NonFinanceTxn.class);
        if (Database.DBS_SUCCESS != retCode) {
            rglog.error("根据系统流水号instId:<{}>,sysSeqNum:<{}>,更新remakr1保留通道编号dstChannelId:<{}>失败<{}>", instId, sysSeqNum, dstChannelId, retCode);
            throw new Exception("更新流水设置通道失败");
        }
    }

}
